﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class PlayerManager : MonoBehaviour {

	//生命值
	public int lifeVal;
	//玩家得分
	public int playerSocre;
    public bool isDead;
    //玩家死亡,重生
    public GameObject born;

    public Text playerGameScore;
    public Text playerGameLife;
    //游戏结束
    public GameObject GameOver;

    //是否失败了
    public bool isDefeat = false;

	//单例
	private static PlayerManager instance;

    public static PlayerManager Instance
    {
        get
        {
            return instance;
        }

        set
        {
            instance = value;
        }
    }

    void Awake() {
        instance = this;
    }

    // Use this for initialization
    void Start () {
       
    }
	
	// Update is called once per frame
	void Update () {
        if (isDefeat) {
            GameOver.SetActive(true);
            Invoke("returnMainScene", 5);
            return;
        }
        if (isDead) {
            Recover();  
        }
        playerGameScore.text = playerSocre.ToString();
        playerGameLife.text = lifeVal.ToString();
    }

    private void Recover() {
        if (lifeVal <= 0)
        {
            //游戏失败返回主界面
            isDefeat = true;
        }
        else {
            //生命值-1之后,玩家重生
            lifeVal--;
            GameObject go = Instantiate(born, new Vector3(-2, -8, 0), Quaternion.identity);
            go.GetComponent<Born>().createPlayer = true;
            isDead = false;
        }
    }

    private void returnMainScene() {
        SceneManager.LoadScene(0);
    }
}
